﻿using System;
using System.Collections.Generic;
using KPIS.DBM;
using System.Configuration;
using KPIS.GERP.GIMS.MODEL;
using System.Data;

namespace KPIS.GERP.GIMS.DAL
{
    public sealed class ReceiveTypeDAL
    {
        IDBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);
        ConvertNullable cn = new ConvertNullable();

        public ReceiveTypeDAL()
        {

        }
        public IList<ReceiveTypeInfo> GetReceiveType(Nullable<int> Sequence, string SearchText)
        {
            string strSQL;
            DataRowCollection drc = null;
            IList<ReceiveTypeInfo> infoList = new List<ReceiveTypeInfo>();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(3);
                dbManager.AddParameters(0, "@Sequence", Sequence);
                dbManager.AddParameters(1, "@SearchText", SearchText);
                dbManager.AddParameters(2, "@ActiveRecord", SystemInfo._ActiveRecordStatus);

                strSQL = " SELECT *"
                        + " FROM ICM_MAS_RECEIVE_TYPE "
                        + " WHERE RECORD_STATUS = @ActiveRecord "
                        + " AND CASE WHEN @SearchText = '' THEN 0 = 0"
                            + " ELSE RECEIVE_TYPE_NAME LIKE CONCAT('%', @SearchText , '%')"
                            + " END"
                        + (Sequence.HasValue ? " AND RECEIVE_TYPE_SEQ = @Sequence" : "") ;

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    for (int i = 0; i < drc.Count; i++)
                    {
                        ReceiveTypeInfo info = new ReceiveTypeInfo();
                        info.Sequence = Convert.ToInt32(drc[i]["receive_type_seq"]);
                        info.Code = drc[i]["receive_type_code"].ToString();
                        info.Name = drc[i]["receive_type_name"].ToString();
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }
    }
}
